package com.neusoft.liduan.d0914;

import java.util.Scanner;

public class CalDemo {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

//		int x = sc.nextInt();
//		System.out.println("请输入运算符mark：");
//		String mark = sc.next();
//		System.out.println("请输入第二个参数y：");
//		int y = sc.nextInt();
//		Cal cal = new Cal(x, y, mark);
//		cal.result();
		Cal cal = new Cal();
		String input = "";// 获取所有输入的内容
		boolean isQuit = false;
		boolean isReset = false;
		while (true) {
			System.out.println("欢迎使用东软计算器：输入（=）计算结果，（Q或q）退出程序，（c或C）重置参数");
			do {
				System.out.println("请输入第一个参数x：");
				input = sc.next();
				isQuit = isQuit(input);
				if (isQuit) {
					break;
				}
				isReset = isReset(input, cal);
				if(isReset) {
					break;
				}
				cal.setX(input);
			} while (cal.getX() == null && !isNumber(cal.getX()));
			if (isQuit) {
				break;
			}
			if(isReset) {
				continue;
			}
			do {
				System.out.println("请输入运算符mark：");
				input = sc.next();
				isQuit = isQuit(input);
				if (isQuit) {
					break;
				}
				isReset = isReset(input, cal);
				if(isReset) {
					break;
				}
				cal.setMark(input);
			} while (cal.getMark() == null);
			if (isQuit) {
				break;
			}
			if(isReset) {
				continue;
			}
			do {
				System.out.println("请输入第二个参数y：");
				input = sc.next();
				isQuit = isQuit(input);
				if (isQuit) {
					break;
				}
				isReset = isReset(input, cal);
				if(isReset) {
					break;
				}
				cal.setY(input);
			} while (cal.getY() == null && !isNumber(cal.getY()));
			if (isQuit) {
				break;
			}
			if(isReset) {
				continue;
			}
			
			input = sc.next();
			if("=".equalsIgnoreCase(input)) {
				cal.result();
			}
		}

//			System.out.println(isNumber(null));
	}
	
	public static boolean isReset(String str, Cal cal) {
		if ("c".equalsIgnoreCase(str)) {
			cal.setX(0 + "");
			cal.setMark(null);
			cal.setY(0 + "");
			return true;
		}
		
		return false;
	}

	public static boolean isQuit(String str) {
		if ("q".equalsIgnoreCase(str)) {
			System.out.println("程序退出");
			return true;
		}

		return false;
	}

	/**
	 * 判断字符串str是否为纯数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
//		for(int i = 0; i < str.length(); i++) {
//			if(str.charAt(i) < '0' || str.charAt(i) > '9') {
//				return false;
//			}
//		}
		if (str == null || str.length() == 0) {// 避免空指针
			return false;
		}

		char[] array = str.toCharArray();
		for (int i = 0; i < array.length; i++) {
			if (array[i] < '0' || array[i] > '9') {
				return false;
			}
		}

		return true;
	}
}
